<?php /*
	
*/ ?>
<?php include("system/config.inc.php");
$donotinclude = 1;
// IF admin is not logged in then it will redirect to the adminlogin page.
if(!isset($_SESSION['adminuserid']))
{
	header("location:index.php?msg=logfirst");
	die();
}

/*$sizeuser="5m";

if(ini_set("upload_max_filesize",$sizeuser))
{
	echo "Changed";
	die();
}
else
{
	echo "Not Changed";
	die();
}*/


// INSERT into database.
if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit")
	{
				
				$txtcountry = mysql_real_escape_string($_REQUEST['gifttitle']);
				$txtisocode2 = mysql_real_escape_string($_REQUEST['giftdesc']);
				$varthpath = $_FILES['imagefile']['name'];
				$txtisocode3 = mysql_real_escape_string($_REQUEST['imgtype']);
				
			if(($_FILES['imagefile']['size'] > 2000000) || ($_FILES['imagefile']['size'] <= 0))
			{
				//echo "Hello";
				//echo $_FILES['imagefile']['size'];						
				header("location:ideas.php?msg=imgszbg");
				die();
			}
			else
			{	
				
				$insert="INSERT INTO `tblideas`
				(`vargiftideatitle` , `vardesc` , `varthimg`, `inttype`,`intstatus`)
				VALUES ('$txtcountry', '$txtisocode2', '$varthpath', '$txtisocode3','0')";
				
				$sql=$obj_db->insert($insert);
				
				// get Id of inserted record
				$screen_id = mysql_insert_id($obj_db->CONN);
				
				//print_r($_FILES);
				
				// Image uploading
				if(isset($_REQUEST['uploading']) && trim($_REQUEST['uploading']) == "imageupload" && 					
				$_FILES['imagefile']['name']!="")
				{
					$targetpath = "../images/ideas/";
					$ext=$_FILES['imagefile']['name'];
					$ext=explode(".",$ext);
					if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG")
					{							
						if($_FILES['imagefile']['size'] <= 2000000)
						{
						echo $_FILES['imagefile']['size'];	
						$filename=$targetpath.$ext[0].$screen_id.".".$ext[1];
						$fl_db=$ext[0].$screen_id.".jpg";
						
							if(file_exists($filename))
							{
								chmod($filename, 0777);
								unlink($filename);
							}
						
						
							if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename))
							{	
								
								$targetpath = "../images/ideas/th/";
								$file=createThumb($filename , $targetpath , $ext[0].$screen_id , $_SESSION['config_val'][0]['intsiteforideasorspecialoffers']);
								$sql= "UPDATE tblideas SET varthimg = '".$fl_db."' WHERE intid = '".$screen_id."' ";
								$res=$obj_db->sql_query($sql);
								chmod($filename, 0777);
								unlink($filename);
							}
						}
						else
						{
							//echo "hai2";
							//die();
							header("location:index.php?msg=imgszbg");
							die();
						}
					}
				}
			}		
				
			header("location:ideas.php?msg=add");
			die();
	}
		

// UPDATE the record
$txtcountry1 = "";
$txtisocode21 = "";
$txtisocode31 = "";

$action=1;

if((isset($_REQUEST['a']) && trim($_REQUEST['a'])==2) && (!(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit")))
{
 	if(isset($_REQUEST['intid']) && trim($_REQUEST['intid'])!="")
 	{
		$screen_id =  $_REQUEST['intid'];
		$sql = "select * from tblideas where intid ='$screen_id'";
		$result = $obj_db->select($sql);
		
		if(count($result)<=0)
		{
			echo " No Record Found!<br>";
			die();
		}
		
		if($result)
		{
			foreach($result as $row)
			{
				$txtcountry1 = $row['vargiftideatitle'];	   
		   		$txtisocode21 = $row['vardesc'];
				$txtisocode31 = $row['varthimg'];
				$txtisocode31 = $row['inttype'];				
				
				$action=2;
			}
		}
		
		if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Update")
		{
				$txtcountry = mysql_real_escape_string($_REQUEST['gifttitle']);
				$txtisocode2 = mysql_real_escape_string($_REQUEST['giftdesc']);
				//$varthpath = $_FILES['imagefile']['name'];				
				$txtisocode3 = mysql_real_escape_string($_REQUEST['imgtype']);
				
			if(($_FILES['imagefile']['size'] > 2000000))
			{
				//echo "Hello";
				//echo $_FILES['imagefile']['size'];
				//die();						
				header("location:ideas.php");
			die();
			}
			else
			{
			
				$sql_upd ="UPDATE tblideas SET  
						`vargiftideatitle` = '$txtcountry',
						`vardesc` = '$txtisocode2',						 
						`inttype` = '$txtisocode3'
						where intid ='$screen_id'";
						 
			//echo $sql_upd;
			//die();
			
			$result = $obj_db->edit($sql_upd);
			$action=1;
			
			// update images
			if(isset($_REQUEST['uploading']) && trim($_REQUEST['uploading']) == "imageupload" &&$_FILES['imagefile']['name']!="")
				{
					$targetpath = "../images/ideas/";
					$ext=$_FILES['imagefile']['name'];
					$ext=explode(".",$ext);
					if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG")
					{							
						if($_FILES['imagefile']['size'] <= 2000000)
						{
						//echo $_FILES['imagefile']['size'];	
						$filename=$targetpath.$ext[0].$screen_id.".".$ext[1];
						$fl_db=$ext[0].$screen_id.".jpg";
						
							if(file_exists($filename))
							{
								chmod($filename, 0777);
								unlink($filename);
							}
							if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename))
							{	
								$targetpath = "../images/ideas/th/";
								$file=createThumb($filename , $targetpath , $ext[0].$screen_id , $_SESSION['config_val'][0]['intsiteforideasorspecialoffers']);
								$sql= "UPDATE tblideas SET varthimg = '".$fl_db."' WHERE intid = '".$screen_id."' ";
								$res=$obj_db->sql_query($sql);
							}
							chmod($filename, 0777);
							unlink($filename);
						}
						else
						{
							header("location:index.php?msg=imgszbg");
							die();
						}
					}
				}
			
			}
			
			header("location:ideas.php?msg=edit");
			die();
		}
	}

}

//DELETE record from database
if(isset($_REQUEST['a']) && trim($_REQUEST['a'])==3)
{
	if(isset($_REQUEST['intid']) && trim($_REQUEST['intid']!=""))
	{	
		$id =  $_REQUEST['intid'];
		$sql= "DELETE FROM `tblideas` WHERE `intid` ='$id'";
		$query1 = mysql_query($sql);
		
		header("location:ideas.php?msg=del");
		die();
	}
}


?>
<?php include("inc/header.php");?>
<body>
<script language="javascript" src="js/ideas.js"></script>
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="1" class="middlebackground">
  <tr>
    <td colspan="3" valign="top" align="center"><?php include(INC."top.inc.php");?></td>
  </tr>
  <tr>
    <td width="2%">&nbsp;</td>
    <td width="95%"><?php   
   if(isset($_REQUEST['script']))
	{
	if(trim($_REQUEST['script'])=='addideas' || trim($_REQUEST['script'])=='editideas')
	{
	?>
		<form action="" method="post" enctype="multipart/form-data" name="ideas">
		
      <table width="64%" border="0" align="center" cellpadding="2" cellspacing="2" class="border">
        <tr class="bg1">
          <td colspan="5" align="center"><strong>
            <?php if($_REQUEST['script']=='addideas'){echo "Add New Gift Ideas...";}
	if($_REQUEST['script']=='editideas'){echo "Edit Gift Ideas...";}?>
          </strong></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="left">&nbsp;</td>
          <td>&nbsp;</td>
          <td align="right">(* All fields are Required)</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td width="2%">&nbsp;</td>
          <td width="29%" align="right" class="sltimg">*Gift Idea Title </td>
          <td width="3%">:</td>
          <td width="63%" align="left"><label>
            <input name="gifttitle" type="text" id="gifttitle" value="<?php echo stripslashes($txtcountry1);?>" />
          </label></td>
          <td width="3%">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="right" class="sltimg">*Description</td>
          <td>:</td>
          <td align="left"><input name="giftdesc" type="text" id="giftdesc" value="<?php echo stripslashes($txtisocode21);?>" /></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td align="right" class="sltimg">*Image</td>
          <td>:</td>
          <td align="left"><input name="imagefile" type="file" id="imagefile" /></td>
          <td>&nbsp;</td>
        </tr>
		<tr>
          <td>&nbsp;</td>
          <td align="right" class="sltimg">*Select Type</td>
          <td>:</td>
          <td align="left">
		  <select name="imgtype" id="imgtype">
		  <?php 
		  $sql = "select * from tblideas where intid ='$screen_id'";
		$result = $obj_db->select($sql);
		$txtisocode3=$result[0]['inttype'];
		?>
            <option value="0">Offer Type</option>
            <option value="1" <?php if($txtisocode3==1){echo "selected";}?>>Special Offer</option>
            <option value="2" <?php if($txtisocode3==2){echo "selected";}?>>Gift Ideas</option>
            </select>            </td>
          <td>&nbsp;</td>
        </tr>

        <tr>
          <td colspan="5" align="left"><input name="uploading" type="hidden" id="uploading" value="imageupload"></td>
        </tr>
        <tr>
          <td colspan="5" align="center"><input name="Submit" type="submit" class="btn" id="Submit" value="<?php echo ($action==2) ? "Update":"Submit"; ?>" onClick="return check();"/></td>
        </tr>
        <tr>
          <td colspan="5" align="center">&nbsp;</td>
        </tr>
      </table>  </form>	
    <?php
	}
}
else
{
?>
<br />
		<br />
		<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2" class="border">
          <tr class="bg1">
            <td colspan="6" align="left"><strong>Gift Ideas...</strong></td>
            <td align="center"><a href="ideas.php?script=addideas" class="aa">[NEW]</a></td>
          </tr>
          <tr>
              <?php 
	   $rows="";
	 if(isset($_REQUEST['page']) && ($_REQUEST['page'])!="")
	 {
	 	$rows=$_REQUEST['page'];
	 
	  }	
	  ?><form name="form1" action="" method="post">
              <td colspan="7" align="left">
			  
			  Row per Page:
                <select name="page" onChange="document.form1.submit();">
                  <option value="0"<?php if($rows=='0'){echo "selected";}else{echo "";}?>>All</option>
                  <option value="5"<?php if($rows=='5'){echo "selected";}else{echo "";}?>>5 Per Page</option>
                  <option value="10"<?php if($rows=='10'){echo "selected";}else{echo "";}?>>10 Per Page</option>
                  <option value="15"<?php if($rows=='15'){echo "selected";}else{echo "";}?>>15 Per Page</option>
                </select>
				
				</td>
				</form>
          </tr>
          <tr class="tblbg3">
            <td align="center"><strong><?php 
			if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphaasc')
			{
		?>
		<a  class="ab1" href="ideas.php?sort=alphadesc">Title&nbsp;<img src="../images/down.gif" border="0" /></a>
		<?php 
			}
			else if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphadesc')
			{
		?><a class="ab1" href="ideas.php?sort=alphaasc">Title&nbsp;<img src="../images/up.gif" border="0" /></a>
		<?php }
			else
			{
			 ?>
			 <a class="ab1" href="ideas.php?sort=alphadesc">Title</a>
			 <?php
		 }?> </strong></td>
			
            <td height="22" align="center" class="tblbg3"><strong>Image</strong></td>
            <td align="center" class="tblbg3"><strong>Type</strong></td>
			<td align="center" class="tblbg3"><strong>View Products</strong></td>
			<td align="center" class="tblbg3"><strong>Status</strong></td>
            <td align="center" class="tblbg3"><strong>Edit</strong></td>
            <td align="center" class="tblbg3"><strong>Delete</strong></td>
          </tr>
<?php 		  
if(isset($_REQUEST['status']))
{
	if(isset($_REQUEST['s']) && trim($_REQUEST['s'])==0)
	 {
		if(isset($_REQUEST['pid']) && trim($_REQUEST['pid']!=""))
		{
			$id=$_REQUEST['pid'];
			$update = $obj_db->sql_query("update tblideas set intstatus = 1 where intid=$id");
			header("location:ideas.php");
			die();
		}
	}
	if(isset($_REQUEST['s']) && trim($_REQUEST['s'])==1)
	{
		if(isset($_REQUEST['pid']) && trim($_REQUEST['pid']!=""))
		{
			$id=$_REQUEST['pid'];
			$update = $obj_db->sql_query("update tblideas set intstatus = 0 where intid=$id");
			header("location:ideas.php");
			die();
		}
	}
}
?>
          <?php	  
	  
	  	/**************************************
				PAGING CODE START
		**************************************/
			//$rowperpage=23;
			$tablename="tblideas";
			$per_page_keywords= "";
			$per_page_sorts="";
			include("system/paging.inc.php");

		/**************************************
				PAGING CODE ENDING
		**************************************/
	
	if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphaasc')
	{
	$sql_select = "select * from tblideas order by `vargiftideatitle` asc Limit ".($page_no*$row_per_page).",".$row_per_page;
	}
	else if(isset($_REQUEST['sort']) && ($_REQUEST['sort'])=='alphadesc')
	{
	$sql_select = "select * from tblideas order by `vargiftideatitle` desc Limit ".($page_no*$row_per_page).",".$row_per_page;
	}
	else
	{
	$sql_select = "select * from tblideas Limit ".($page_no*$row_per_page).",".$row_per_page;
	}
	$result = $obj_db->select($sql_select);
	if($result)
	{

	for($i=0;$i<count($result);$i++)
	 { 
		$id=$result[$i]['intid'];
		
?>
          <tr class="<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>" onMouseOver="this.className='Hrhover';"  onMouseOut="this.className='<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>';">
            <td align="center"><?php echo stripslashes($result[$i]['vargiftideatitle']);?></td>
			
            <td align="center"><img title="'<?php echo $result[$i]['vargiftideatitle'];?>'" src="../images/ideas/th/<?php echo $result[$i]['varthimg'];?>"/></td>
            <td align="center"><?php 
			if($result[$i]['inttype']=="1")
			{
				echo "Special Offer";
			} 
			if($result[$i]['inttype']=="2")
			{
				echo "Gift Idea";	
			}
			
			?></td>
			<td align="center"><a class="aa" Title="Click here to View Products" href="listproduct.php?prid=<?php echo $id;?>">View Products</a></td>
			<td align="center"><table border="0" cellspacing="0" cellpadding="0" align="center">
				  <tr>
		<?php 
					if($result[$i]['intstatus']==0)
					{
						
						echo "<td><a Title='Click here to Show offer' class='aa' href='ideas.php?status=0&s=0&pid=$id'>Show</a></td>";		
					}
					if($result[$i]['intstatus']==1)
					{
						
						echo "<td><a Title='Click here to Hide offer' class='aa' href='ideas.php?status=1&s=1&pid=$id'>Hide</a></td>";
					}
				?>
				</tr></table></td>
            <td align="center"><a class="link" Title="Click here to Edit" href="ideas.php?script=editideas&amp;a=2&amp;intid=<?php echo $result[$i]['intid']; ?>"><img src="images/edit.bmp" border="0"/></a></td>
            <td align="center"><a Title="Click here to Delete" href="ideas.php?a=3&amp;intid=<?php echo $result[$i]['intid']; ?>" onClick="return confirm('Are you sure to delete this record?');"><img src="images/delete.bmp" border="0"/></a></td>
          </tr>
          <?php }}?>
          <tr>
            <td colspan="6" align="center"><?php
pagindet_atbotttom_page($div_page_no,$page_no,$req_querystr,$total_db_rec,$row_per_page);
?></td>
          </tr>
      </table>
		<?php }?>
    </td>
    <td width="3%">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><?php include("inc/bottom.php");?></td>
  </tr>
</table>


